package com.jiangjiang.eshop.schedule.stock;

import com.jiangjiang.eshop.schedule.domain.GoodsAllocationStockEntity;
import com.jiangjiang.eshop.schedule.domain.GoodsStockEntity;
import com.jiangjiang.eshop.schedule.service.ScheduleGoodsAllocationStockService;
import com.jiangjiang.eshop.schedule.service.ScheduleGoodsStockService;
import com.jiangjiang.eshop.schedule.service.ScheduleGoodsAllocationStockDetailService;
import com.jiangjiang.eshop.wms.dto.GoodsAllocationStockDetailDTO;
import com.jiangjiang.eshop.wms.dto.PurchaseInputOrderDTO;
import com.jiangjiang.eshop.wms.dto.PurchaseInputOrderItemDTO;
import com.jiangjiang.eshop.wms.dto.PurchaseInputOrderPutOnItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName PurchaseInputScheduleStockUpdater
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/30 22:29
 * @Version 1.0
 **/
@Component
@Scope("prototype")
public class PurchaseInputScheduleStockUpdater extends AbstractScheduleStockUpdater {

    private PurchaseInputOrderDTO purchaseInputOrderDTO;

    @Autowired
    private ScheduleGoodsStockService scheduleGoodsStockService;
    @Autowired
    private ScheduleGoodsAllocationStockDetailService scheduleGoodsAllocationStockDetailService;
    @Autowired
    private ScheduleGoodsAllocationStockService scheduleGoodsAllocationStockService;


    @Override
    protected void updateGoodsStock() throws Exception {

        List<PurchaseInputOrderItemDTO> items = purchaseInputOrderDTO.getItems();
        Map<Long, Long> skuArrivalCountMap = items.stream().collect(
                Collectors.groupingBy(PurchaseInputOrderItemDTO::getGoodsSkuId,
                        Collectors.summingLong(PurchaseInputOrderItemDTO::getArrivalCount)));

        Set<Long> skuIds = items.stream()
                .map(PurchaseInputOrderItemDTO::getGoodsSkuId)
                .collect(Collectors.toSet());

        List<GoodsStockEntity> entities = scheduleGoodsStockService.queryBySkuIds(skuIds).stream().peek(
                entity -> entity.setAvailableStockQuantity(entity.getAvailableStockQuantity() +
                        skuArrivalCountMap.getOrDefault(entity.getGoodsSkuId(), 0L)))
                .collect(Collectors.toList());
        // 批量更新数据
        scheduleGoodsStockService.updateBatchById(entities);
    }

    @Override
    protected void updateGoodsAllocationStock() throws Exception {

        for (PurchaseInputOrderItemDTO item : purchaseInputOrderDTO.getItems()) {
            List<PurchaseInputOrderPutOnItemDTO> putOnItems = item.getPutOnItems();
            for (PurchaseInputOrderPutOnItemDTO putOnItem : putOnItems) {
                GoodsAllocationStockEntity entity = scheduleGoodsAllocationStockService.queryBySkuId(putOnItem.getGoodsAllocationId(), putOnItem.getGoodsSkuId());
                entity.setAvailableStockQuantity(entity.getAvailableStockQuantity() + putOnItem.getPutOnShelvesCount());
                scheduleGoodsAllocationStockService.updateById(entity);
            }
        }

    }

    @Override
    protected void updateGoodsAllocationStockDetail() throws Exception {
        for (PurchaseInputOrderItemDTO item : purchaseInputOrderDTO.getItems()) {
            List<GoodsAllocationStockDetailDTO> stockDetails = item.getStockDetails();
            scheduleGoodsAllocationStockDetailService.saveBatchDetailDTO(stockDetails);
        }

    }


    @Override
    public void setParameter(Object parameter) {
        this.purchaseInputOrderDTO = (PurchaseInputOrderDTO) parameter;
    }
}
